/* ***********************************************************************
 * 项目名称:油耗
 * 项目组:
 * 创建时间:2017.7.11
 * 创建人:马方印
 * 用途:油罐collectionViewCell
 * 版本:1.0
 * 修改记录:    修改内容   修改原因   修改人   修改时间
 *               无        无       无       无
 *************************************************************************/

#import "YZOilTankCell.h"
#import "YZInputOrOutputGasolineModel.h"

@implementation YZOilTankCell

- (instancetype)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    
    if (self) {
        
        [self.contentView addSubview:self.oilBackView];
        [self.oilBackView addSubview:self.blueImageView];
        [self.oilBackView addSubview:self.whiteImageView];
        [self.oilBackView addSubview:self.typeLabel];
        [self.oilBackView addSubview:self.percentageLabel];
        [self.oilBackView addSubview:self.litreLabel];
        
        [self.contentView addSubview:self.showBackImageView];
        [self.contentView addSubview:self.showTitleLabel];
        [self.contentView addSubview:self.showLitreLabel];
        
        [self setSubViewsFrame];
        
    }
    
    return self;
}

#pragma mark - 设置数据源
- (void)setListModel:(YZListModel *)listModel{
    _listModel = listModel;
    
    self.typeLabel.text = listModel.iolnum;
    self.litreLabel.text = [NSString stringWithFormat:@"当前油量:%.1fL",[listModel.iolafter floatValue]];
    self.showLitreLabel.text = [NSString stringWithFormat:@"%.1fL",[listModel.total floatValue]];
    CGFloat percentage = [listModel.ioltotal floatValue];//百分比
    self.percentageLabel.text = [NSString stringWithFormat:@"%.2f%%",percentage];
    
    if ((percentage*0.01) > 0.01) {
        //油柱高度
        [self.blueImageView mas_updateConstraints:^(MASConstraintMaker *make) {
            
            make.top.mas_equalTo(self.contentView.mas_top).with.offset(25-340*percentage*0.01);//经测试标准的显示底部的话 就应该上移25
        }];
        //百分比位置
        [self.percentageLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            
            make.bottom.mas_equalTo(self.oilBackView.mas_bottom).with.offset(30-340*percentage*0.01);
        }];
        
        //防止百分比与测量label重叠 所以把测量超过一半显示放在询问
        if (percentage * 0.01 > 0.45) {
            
            [self.litreLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
                
                make.centerX.mas_equalTo(self.oilBackView.mas_centerX);
                make.bottom.mas_equalTo(self.oilBackView.mas_bottom).with.offset(-25);
            }];
        }
        if (percentage * 0.01 > 0.8) {
            
            //百分比位置
            [self.percentageLabel mas_updateConstraints:^(MASConstraintMaker *make) {
                
                make.bottom.mas_equalTo(self.oilBackView.mas_bottom).with.offset(-170);
            }];
        }
    }
    
    //设置总油量标题显示
    if ([self.currentType isEqualToString:@"input"]) {
      
        self.showTitleLabel.text = @"入油量";
    }
    else if ([self.currentType isEqualToString:@"output"]){
        
        self.showTitleLabel.text = @"出油量";
    }
}

#pragma mark - 布局
- (void)setSubViewsFrame{
    
    [self.oilBackView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.contentView.mas_top).with.offset(0);
        make.centerX.mas_equalTo(self.contentView.mas_centerX);
        make.width.mas_equalTo(@155);
        make.height.mas_equalTo(@340);
    }];
    
    [self.whiteImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.mas_equalTo(self.oilBackView);
    }];
    
    [self.blueImageView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.mas_equalTo(self.contentView.mas_top).with.offset(-25);//经测试标准的显示底部的话 就应该上移25
        make.centerX.mas_equalTo(self.contentView.mas_centerX);
        make.width.mas_equalTo(@155);
        make.height.mas_equalTo(@680);
    }];
    
    [self.typeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.mas_equalTo(self.oilBackView.mas_top).with.offset(10);
        make.centerX.mas_equalTo(self.oilBackView.mas_centerX);
    }];
    
    [self.litreLabel mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.centerX.mas_equalTo(self.oilBackView.mas_centerX);
        make.centerY.mas_equalTo(self.oilBackView.mas_centerY);
    }];
    
    [self.percentageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.bottom.mas_equalTo(self.oilBackView.mas_bottom).with.offset(-15);
        make.centerX.mas_equalTo(self.oilBackView.mas_centerX);
    }];
    
    [self.showBackImageView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.mas_equalTo(self.oilBackView.mas_bottom).with.offset(20);
        make.centerX.mas_equalTo(self.blueImageView.mas_centerX);
        make.width.mas_equalTo(@100);
        make.height.mas_equalTo(@100);
    }];
    
    [self.showTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.mas_equalTo(self.showBackImageView.mas_top).with.offset(30);
        make.centerX.mas_equalTo(self.showBackImageView.mas_centerX);
        make.height.mas_equalTo(20);
    }];
    
    [self.showLitreLabel mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.mas_equalTo(self.showTitleLabel.mas_bottom).with.offset(0);
        make.centerX.mas_equalTo(self.showBackImageView.mas_centerX);
        make.height.mas_equalTo(20);
    }];
}

#pragma mark - 懒加载 入油出油显示
- (UILabel *)showLitreLabel{
    
    if (!_showLitreLabel) {
        
        _showLitreLabel = [[UILabel alloc]init];
        _showLitreLabel.text = @"0.000L";
        _showLitreLabel.font = [UIFont systemFontOfSize:16.0f];
        _showLitreLabel.textColor = [UIColor whiteColor];
        _showLitreLabel.textAlignment = NSTextAlignmentCenter;
    }
    
    return _showLitreLabel;
}

- (UILabel *)showTitleLabel{
    
    if (!_showTitleLabel) {
        
        _showTitleLabel = [[UILabel alloc]init];
        _showTitleLabel.text = @"入油量";
        _showTitleLabel.font = [UIFont systemFontOfSize:16.0f];
        _showTitleLabel.textColor = [UIColor whiteColor];
        _showTitleLabel.textAlignment = NSTextAlignmentCenter;
    }
    
    return _showTitleLabel;
}

- (UIImageView *)showBackImageView{
    
    if (!_showBackImageView) {
        
        _showBackImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"bg_blue"]];
    }
    
    return _showBackImageView;
}

#pragma mark - 懒加载 油罐
- (UIImageView *)whiteImageView{
    
    if (!_whiteImageView) {
        
        _whiteImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"iol_bg1"]];
    }
    
    return _whiteImageView;
}

- (UIImageView *)blueImageView{
    
    if (!_blueImageView) {
        
        _blueImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"iol_bg2"]];
    }
    
    return _blueImageView;
}

- (UIView *)oilBackView{
    
    if (!_oilBackView) {
        
        _oilBackView = [[UIView alloc]init];
        _oilBackView.backgroundColor = [UIColor whiteColor];
        _oilBackView.clipsToBounds = YES;
    }
    
    return _oilBackView;
}

- (UILabel *)litreLabel{
    
    if (!_litreLabel) {
        
        _litreLabel = [[UILabel alloc]init];
        _litreLabel.text = @"当前油量:0.000升";
        _litreLabel.font = [UIFont systemFontOfSize:16.0f];
        _litreLabel.textColor = [UIColor whiteColor];
    }
    
    return _litreLabel;
}

- (UILabel *)percentageLabel{
    
    if (!_percentageLabel) {
        
        _percentageLabel = [[UILabel alloc]init];
        _percentageLabel.text = @"0%";
        _percentageLabel.font = [UIFont systemFontOfSize:16.0f];
        _percentageLabel.textColor = [UIColor whiteColor];
    }
    
    return _percentageLabel;
}

- (UILabel *)typeLabel{
    
    if (!_typeLabel) {
        
        _typeLabel = [[UILabel alloc]init];
        _typeLabel.text = @"3罐";
        _typeLabel.font = [UIFont systemFontOfSize:18.0f];
        _typeLabel.textColor = [UIColor whiteColor];
    }
    
    return _typeLabel;
}

@end
